commonlibsse_ng\re\b/
BSTCaseInsensitiveStringMap.rs

1//! # BSTCaseInsensitiveStringMap
2//!
3//! This module defines the `BSTCaseInsensitiveStringMap<T>` and `NiTStringMap<T>` structs,
4//! simulating the case-insensitive string map and its base map functionality.
5
6use std::hash::{Hash, Hasher};
7
8use crate::re::NiTStringMap::NiTStringMap;
9
10/// Represents a case-insensitive string map.
11#[repr(C)]
12#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
13pub struct BSTCaseInsensitiveStringMap<V> {
14    pub __base: NiTStringMap<V>,
15}
16
17const _: () = {
18    type ParentType = [u8; 0x20];
19    assert!(core::mem::offset_of!(BSTCaseInsensitiveStringMap::<ParentType>, __base) == 0x00);
20    assert!(core::mem::size_of::<BSTCaseInsensitiveStringMap::<ParentType>>() == 0x28);
21};
22
23/// Trait to represent map operations.
24pub trait BSTCaseInsensitiveStringMapTrait<T> {
25    /// Computes the hash of the key in a case-insensitive manner.
26    ///
27    /// # Arguments
28    /// - `key`: The string key.
29    ///
30    /// # Returns
31    /// - The hash value.
32    fn hash_function(&self, key: &str) -> u32;
33
34    /// Checks if two keys are equal in a case-insensitive manner.
35    ///
36    /// # Arguments
37    /// - `lhs`: The left-hand side key.
38    /// - `rhs`: The right-hand side key.
39    ///
40    /// # Returns
41    /// - `true` if equal, `false` otherwise.
42    fn key_eq(&self, lhs: &str, rhs: &str) -> bool;
43}
44
45impl<T> BSTCaseInsensitiveStringMapTrait<T> for BSTCaseInsensitiveStringMap<T> {
46    #[inline]
47    fn hash_function(&self, key: &str) -> u32 {
48        // Perform case-insensitive hashing
49        let mut hasher = std::collections::hash_map::DefaultHasher::new();
50        key.to_lowercase().hash(&mut hasher);
51        hasher.finish() as u32
52    }
53
54    #[inline]
55    fn key_eq(&self, lhs: &str, rhs: &str) -> bool {
56        lhs.eq_ignore_ascii_case(rhs)
57    }
58}
59
60/// The virtual function table for `BSTCaseInsensitiveStringMap<T>`.
61///
62/// This struct defines function pointers to simulate the C++ virtual functions.
63#[repr(C)]
64pub struct BSTCaseInsensitiveStringMapVtbl<T> {
65    /// Destructor function pointer.
66    pub CxxDrop: fn(this: &mut BSTCaseInsensitiveStringMap<T>),
67
68    /// Function pointer for hashing the key.
69    pub HashFunction: fn(this: &BSTCaseInsensitiveStringMap<T>, key: &str) -> u32,
70
71    /// Function pointer for case-insensitive key equality.
72    pub KeyEq: fn(this: &BSTCaseInsensitiveStringMap<T>, lhs: &str, rhs: &str) -> bool,
73}